package com.gitee.srurl.core.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.srurl.core.domain.SucUrlRecordDomain;
import com.gitee.srurl.core.dto.RefreshRemainRecordDTO;
import com.gitee.srurl.core.dto.result.ShortUrlResultDTO;
import com.gitee.srurl.core.exception.ServiceException;
import com.gitee.srurl.core.mapper.SucUrlRecordMapper;
import com.gitee.srurl.core.service.ISucUrlRecordService;
import com.gitee.srurl.core.util.PageQuery;
import com.gitee.srurl.core.util.RedisCacheUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zxw
 * @date 2023/4/29 13:50
 */
@Service
@RequiredArgsConstructor
public class SucUrlRecordServiceImpl extends ServiceImpl<SucUrlRecordMapper, SucUrlRecordDomain> implements ISucUrlRecordService {

    @Override
    public IPage<SucUrlRecordDomain> getList(PageQuery pageQuery, SucUrlRecordDomain urlRecordDomain) {

        LambdaQueryWrapper<SucUrlRecordDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(urlRecordDomain.getOriginUrl()), SucUrlRecordDomain::getOriginUrl, urlRecordDomain.getOriginUrl())
                .like(StrUtil.isNotBlank(urlRecordDomain.getShortId()), SucUrlRecordDomain::getShortId, urlRecordDomain.getShortId())
                .eq(StrUtil.isNotBlank(urlRecordDomain.getDisable()), SucUrlRecordDomain::getDisable, urlRecordDomain.getDisable())
                .orderByDesc(SucUrlRecordDomain::getCreateTime);

        return this.page(pageQuery.build(), queryWrapper);
    }

    @Override
    public void add(SucUrlRecordDomain urlRecordDomain, ShortUrlResultDTO shortUrl) {

        if (shortUrl == null || shortUrl.getSuccess() == null || !shortUrl.getSuccess()) {
            throw new ServiceException("短网址生成失败!");
        }
        // todo 剩余处理 目前没有其他处理
    }

    @Override
    public void updateUrlRecord(SucUrlRecordDomain urlRecordDomain, ShortUrlResultDTO shortUrl) {

        if (shortUrl == null || shortUrl.getSuccess() == null || !shortUrl.getSuccess()) {
            throw new ServiceException("短网址生成失败!");
        }
        // todo 剩余处理 目前没有其他处理
    }

    @Override
    public void disabledOrActive(Long id) {

        SucUrlRecordDomain urlRecordDomain = this.getById(id);
        if (urlRecordDomain == null) {
            throw new ServiceException("未查询到对应的数据信息！");
        }

        String disable = urlRecordDomain.getDisable();
        if (StrUtil.equals(disable, "0")) {
            // 说明要禁用
            urlRecordDomain.setDisable("1");
        } else {
            urlRecordDomain.setDisable("0");
        }
        RedisCacheUtil.removeCache(urlRecordDomain.getShortId());
        try {
            this.updateById(urlRecordDomain);
        } finally {
            // 延迟删除一遍
            RedisCacheUtil.removeCache(urlRecordDomain.getShortId(), true);
        }
    }

    @Override
    public List<SucUrlRecordDomain> getRecentOneDayHadVisitRecordNonUnlimited() {

        return this.getBaseMapper().selectRecentOneDayHadVisitRecordNonUnlimited();
    }

    @Override
    public void refreshRecordRemainTimes(List<RefreshRemainRecordDTO> refreshRemainRecordDTOList) {

        if (CollectionUtil.isEmpty(refreshRemainRecordDTOList)) {
            return;
        }

        for (RefreshRemainRecordDTO refreshRemainRecordDTO : refreshRemainRecordDTOList) {
            this.getBaseMapper().refreshRecordRemainTimes(refreshRemainRecordDTO);
        }
    }
}
